﻿using Biomedica.Graphics;
using KinectWindows.Math;
using Microsoft.Kinect;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace Biomedica.Screen.Mappers
{
    
    public class ScreencordMapper
    {
        private Canvas _canvas;
        public double XBound { set; get; }
        public double YBound { set; get; }
        public double SmothingFactor { set; get; }

        public double X {get;set;}
        public double Y { get; set; }


        double SX;
        double SY;
        double MaxX;
        double MaxY;
        double thetax;
        double thetay;
        public ScreencordMapper(Canvas canvas)
        {
            _canvas = canvas;
            XBound = 32;
            YBound = 32;
            SmothingFactor = 1;

             SX = KinectWindows.WindowManager.WindowManagerLooper.Scr.ScreenCanvas.ActualWidth;
             SY = KinectWindows.WindowManager.WindowManagerLooper.Scr.ScreenCanvas.ActualHeight;

             MaxX = KinectWindows.WindowManager.WindowManagerLooper.Scr.RangeX;

             MaxY = KinectWindows.WindowManager.WindowManagerLooper.Scr.RangeY;
                       

              thetax = SX / MaxX;
              thetay = SY / MaxY;
        }
        public Vector2 MapKinecktToScreen(Vector3 Center, Vector3 _joint)
        {
            double dx = (KinectWindows.WindowManager.WindowManagerLooper.Scr.ScreenCenter.X ) - ((Center.X - _joint.X) * thetax);
            double dy = (KinectWindows.WindowManager.WindowManagerLooper.Scr.ScreenCenter.Y) - ((Center.Y - _joint.Y) * thetay);
            
            if (dx >= SX) dy = SY; 
            if (dy >= SY) dy = SY;

            if (dx <= 0) dy = 0;
            if (dy <= 0) dy = 0; 

            return new Vector2((float) dx,(float) dy);

        }
        public void MapScreenToScreen(float Sx, float Sy, float x, float y)
        {
            float W =(float)( _canvas.ActualWidth *SmothingFactor);
            float H = (float)(_canvas.ActualHeight *SmothingFactor);

            float deltaX =x-Sx;
            float deltaY =Sy - y;
            int direction=1;
           

            X = (direction*/*(W/2)+ */ (W * deltaX));
            Y = (/*(H/2) +*/(H* deltaY)) ;

            if (X > _canvas.ActualWidth - XBound || X <= XBound)
            {
                if (X > _canvas.ActualWidth - XBound)
                    X = _canvas.ActualWidth - XBound;
                else
                    X = 32;
            }
            if (Y >= _canvas.ActualHeight - YBound || Y <= YBound)
            {
                if (Y > _canvas.ActualHeight - YBound)
                    Y = _canvas.ActualHeight - YBound;
                else
                    Y = 32;
            }
        }

          

    }
}
